/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package custom.log4j;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;
import javax.swing.JTextArea;

/**
 *
 * @author david
 */
public class JTextAreaAppender extends AppenderSkeleton {

    private JTextArea textArea = null;

    public JTextAreaAppender() {
    }

    public JTextAreaAppender(final JTextArea textArea) {
        setTextArea(textArea);
    }

    public void setTextArea(final JTextArea textArea) {
        this.textArea = textArea;
    }

    public JTextArea getTextArea() {
        return textArea;
    }

    protected void append(LoggingEvent le) {
        String logOutput = this.layout != null ? this.layout.format(le) : (String) le.getMessage();

        if (textArea != null) {
            textArea.append(logOutput);
        }

        if (this.layout != null && this.layout.ignoresThrowable()) {
            String[] lines = le.getThrowableStrRep();
            if (lines != null) {
                for (String line : lines) {
                    textArea.append(line + System.getProperty("line.separator"));
                }
            }
        }
        textArea.setCaretPosition(textArea.getDocument().getLength());
    }

    public boolean requiresLayout() {
        return true;
    }

    public void close() {
    }
}
